<template>
  <div class="container">
    <div class="app-container">
      <div class="role-operate">
        <el-button type="primary" @click="showDialog">添加角色 </el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="tableData" style="width: 100%">
        <!-- 放置表格列 -->
        <el-table-column prop="roleName" align="center" label="角色">
          <template v-slot="{ row }">
            <el-input v-if="row.isEdit" size="mini" />
            <span v-else>{{ row.roleName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" align="center" label="启用" width="180">
          <template v-slot="{ row }">
            <span v-if="!row.isEdit">{{ row.status==='0' ? '启用' : '停用' }}</span>
            <el-switch v-else v-model="roleForm.status" active-value="0" inactive-value="1" size="mini" />
          </template>
        </el-table-column>
        <el-table-column prop="roleSort" align="center" label="显示顺序" width="180">
          <template v-slot="{ row }">
            <span v-if="!row.isEdit">{{ row.roleSort }}</span>
            <el-input-number v-else v-model="roleForm.roleSort" :min="0" :max="100" size="mini" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <template v-if="row.isEdit">
              <!-- 编辑状态 -->
              <el-button type="primary" size="mini">确定</el-button>
              <el-button size="mini" @click="btnCancelRow(row)">取消</el-button>
            </template>
            <template>
              <el-button type="text" size="mini" @click="btnEditRow(row)">编辑</el-button>
              <el-button type="text" size="mini">删除</el-button>
              <el-button type="text" size="mini">分配权限</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" justify="end" align="middle" style="height: 60px;">
        <!-- 放置分页组件 -->
        <el-pagination
          :page-size="pageParams.pageSize"
          :current-page="pageParams.pageNum"
          :total="pageParams.total"
          layout="prev, pager, next"
          @current-change="changePage"
        />
      </el-row>
    </div>
    <!-- 放置dialog组件 -->
    <el-dialog title="新增角色" :visible.sync="dialogVisible" width="500px" @close="btnCancel">
      <el-form ref="roleForm" label-width="120px" :model="roleForm" :rules="rules">
        <el-form-item prop="roleName" label="角色名称">
          <el-input v-model="roleForm.roleName" size="mini" />
        </el-form-item>
        <el-form-item prop="roleKey" label="权限字符">
          <el-input v-model="roleForm.roleKey" size="mini" />
        </el-form-item>
        <el-form-item prop="status" label="启用">
          <el-switch v-model="roleForm.status" active-value="0" inactive-value="1" size="mini" />
        </el-form-item>
        <el-form-item prop="roleSort" label="显示顺序">
          <el-input-number v-model="roleForm.roleSort" :min="0" :max="100" size="mini" />
        </el-form-item>
        <el-form-item prop="remark" label="备注">
          <el-input v-model="roleForm.remark" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-row justify="center" type="flex">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="btnOK">确定</el-button>
              <el-button size="mini" @click="btnCancel">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleList, addRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      order: 0,
      dialogVisible: false,
      tableData: [],
      pageParams: {
        pageSize: 5,
        pageNum: 1,
        total: 0
      },
      roleForm: {
        roleName: '',
        roleKey: '',
        roleSort: '',
        status: 0,
        remark: ''
      },
      rules: {
        roleName: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        roleKey: [{ required: true, message: '权限字符不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.pageParams)
      this.tableData = rows
      this.tableData.forEach(item => {
        this.$set(item, 'isEdit', false)
      })
      this.pageParams.total = total
    },
    changePage(pageNum) {
      this.pageParams.pageNum = pageNum // 赋值给pageParams的pageNum
      this.getRoleList() // 调用获取数据的方法
    },
    showDialog() {
      this.dialogVisible = true
    },
    btnOK() {
      this.$refs.roleForm.validate(
        async isOK => {
          if (isOK) {
            // todo 关联菜单
            await addRole(this.roleForm)
            this.getRoleList()
            this.btnCancel()
          }
        }
      )
    },
    btnCancel() {
      this.$refs.roleForm.resetFields()
      this.dialogVisible = false
    },
    btnEditRow(row) {
      row.isEdit = true
    },
    btnCancelRow(row) {
      row.isEdit = false
    }
  }
}
</script>
<style>
.role-operate {
  padding: 10px;
}
</style>
